﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;

namespace Booking_Hotel
{
    public partial class Supplier_DichVu : System.Web.UI.Page
    {
        Supplier_Hotel_DichVu entities = new Supplier_Hotel_DichVu();
        ThanhBHotelDataContext TBK = new ThanhBHotelDataContext();
        int Supplier_Hotel_DichVuID;
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                Session["ss_SupplierID"] = 2;//Gan SupplierID cho Dropdownlist de lay DS khach san cua Supplier
                ShowDichVu();
            }
            
        }

        protected void Bt_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                entities = new Supplier_Hotel_DichVu();
                entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                entities.TenDichVu = txt_TenDichVu.Text.Trim();
                entities.Description = txt_Desciption.Text.Trim();
                entities.GiaDichVu = float.Parse(txt_GiaDichVu.Text.Trim());
                Supplier_DichVuDAO sd = new Supplier_DichVuDAO();
                sd.addSupplier_Hotel_DichVu(entities);
                ShowDichVu();

            }
            catch (Exception)
            {

                throw;
            }

        }
        protected void ShowDichVu()
        {
            var dichvu = from dv in TBK.Supplier_Hotels
                         join shd in TBK.Supplier_Hotel_DichVus on dv.Supplier_HotelID equals shd.Supplier_HotelID
                         where dv.SupplierID == (int)Session["ss_SupplierID"]
                         select new
                         {
                          Supplier_Hotel_DichVuID=  shd.Supplier_Hotel_DichVuID,
                          Supplier_HotelID = shd.Supplier_HotelID,
                          TenKhachSan=  dv.TenKhachSan,
                          TenDichVu=  shd.TenDichVu,
                          Description= shd.Description,
                          GiaDichVu= shd.GiaDichVu
                         };
            GridView1.DataSource = dichvu;
            GridView1.DataBind();
        
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow select = GridView1.Rows[e.NewSelectedIndex];
            Session["ss_Supplier_Hotel_DichVuID"] = int.Parse(select.Cells[0].Text.Trim());
            DDL_TenKhachSan.SelectedValue = select.Cells[1].Text.Trim();
            txt_TenDichVu.Text = select.Cells[3].Text.Trim();
            txt_Desciption.Text = select.Cells[4].Text.Trim();
            txt_GiaDichVu.Text = select.Cells[5].Text.Trim();
            
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton bt_Delete = sender as ImageButton;
            GridViewRow row = (GridViewRow)bt_Delete.NamingContainer;
            Supplier_Hotel_DichVuID = int.Parse(row.Cells[0].Text.Trim());//Supplier_Hotel_DichVuID trên lưới.
            Supplier_Hotel_DichVu shd = TBK.Supplier_Hotel_DichVus.Where(c => c.Supplier_Hotel_DichVuID == Supplier_Hotel_DichVuID).Single();
            TBK.Supplier_Hotel_DichVus.DeleteOnSubmit(shd);
            TBK.SubmitChanges();
            ShowDichVu();
        }

        protected void Bt_CapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                int check_Supplier_Hotel_DichVuID = (int)(Session["ss_Supplier_Hotel_DichVuID"]);
                if (check_Supplier_Hotel_DichVuID != null)
                {
                    entities = new Supplier_Hotel_DichVu();
                    entities.Supplier_Hotel_DichVuID = check_Supplier_Hotel_DichVuID;
                    entities.Supplier_HotelID = int.Parse(DDL_TenKhachSan.SelectedValue);
                    entities.TenDichVu = txt_TenDichVu.Text.Trim();
                    entities.Description = txt_Desciption.Text.Trim();
                    entities.GiaDichVu = float.Parse(txt_GiaDichVu.Text.Trim());
                    Supplier_DichVuDAO sd = new Supplier_DichVuDAO();
                    sd.updateSupplier_Hotel_DichVu(entities);
                    ShowDichVu();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Vui lòng chọn đối tượng cần update dưới lưới", this.Page);
                     
                }
                      
            }
            catch (Exception)
            {
                
                throw;
            }
        }
     
    }
}